package com.hzit.alipayfront.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.hzit.alipayfront.config.AlipayConfig;
import com.hzit.paycommon.paycommonapi.resp.RespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/alipay/refund")
public class AlipayRefundController {

    AlipayConfig alipayConfig = new AlipayConfig();


    /**
     * 支付宝D退款
     */
    @RequestMapping("/tradeRefund")
    public RespVo tradePre(){

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getUrl(),alipayConfig.getAppId(),
                alipayConfig.getPrivateKey(),"json","GBK",alipayConfig.getAlipayPubKey(),"RSA2");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

//

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no","1298525297121038336");
        jsonObject.put("refund_amount","90");
        jsonObject.put("refund_reason","正常退款");
        jsonObject.put("out_request_no",System.currentTimeMillis());
        request.setBizContent(jsonObject.toJSONString());


        try {
            AlipayTradeRefundResponse response = alipayClient.execute(request);

            System.out.println("----------getSubCode---------"+ response.getSubCode());
            System.out.println("----------getMsg---------"+ response.getMsg());
            System.out.println("----------getFundChange---------"+ response.getFundChange());
            System.out.println("-----------getTradeNo--------"+ response.getTradeNo());
            System.out.println("----------getFundChange---------"+ response.getRefundFee());

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


        return null;
    }



    public static void main(String[] args) {
        new AlipayRefundController().tradePre();
    }


}
